package ihm;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import metier.Film;
import metier.Horaire;
import metier.Salle;
import metier.Seance;
import metier.Tarif;
import dao.DaoProjetCinema;

public class FenetreAjoutSeance extends JInternalFrame implements ActionListener
{
	private JTextField txtJour;
	private JTextField txtDate;
	private JTextField NbrePlacesAchetees;
	private JLabel lblJour;
	private JLabel lblDate;
	private JLabel lblNombreDePlaces;
	private JButton btnAjouter;
	private Vector<Seance> lesSeances;
	private Salle uneSalle;
	private Film unFilm;
	private Tarif unTarif;
	private Horaire unHoraire;
	private JComboBox <Tarif>lstTarif;
	private JComboBox <Film>lstFilm;
	private JComboBox <Salle>lstSalle;
	private JComboBox <Horaire>lstHoraire;
	private Vector<Tarif> lesTarifs;
	private Vector<Horaire> lesHoraires;
	private Vector<Salle> lesSalles;
	private Vector<Film> lesFilms;
	
	/**
	 * Create the frame.
	 */
	public FenetreAjoutSeance(Vector <Seance> lesSeances, Vector<Tarif> lesTarifs, Vector<Film> lesFilms, Vector<Salle> lesSalles, Vector<Horaire> lesHoraires)
	{
		getContentPane().setLayout(null);
		
		this.txtJour = new JTextField();
		this.txtJour.setBounds(208, 67, 117, 20);
		getContentPane().add(txtJour);
		this.txtJour.setColumns(10);
		
		this.txtDate = new JTextField();
		this.txtDate.setBounds(208, 98, 117, 20);
		getContentPane().add(txtDate);
		this.txtDate.setColumns(10);
		
		this.NbrePlacesAchetees = new JTextField();
		this.NbrePlacesAchetees.setBounds(208, 129, 117, 20);
		getContentPane().add(NbrePlacesAchetees);
		this.NbrePlacesAchetees.setColumns(10);
		
		this.lblJour = new JLabel("Jour :");
		this.lblJour.setBounds(38, 70, 93, 14);
		getContentPane().add(lblJour);
		
		this.lblDate = new JLabel("Date :");
		this.lblDate.setBounds(38, 101, 46, 14);
		getContentPane().add(lblDate);
		
		this.lblNombreDePlaces = new JLabel("Nombre de places achet\u00E9es :");
		this.lblNombreDePlaces.setBounds(38, 132, 196, 14);
		getContentPane().add(lblNombreDePlaces);
		
		this.btnAjouter = new JButton("Ajouter");
		this.btnAjouter.addActionListener(this);
		this.btnAjouter.setBounds(313, 308, 91, 23);
		getContentPane().add(btnAjouter);
		
		this.lstSalle = new JComboBox<Salle>(lesSalles);
		this.lstSalle.setBounds(208, 160, 117, 20);
		getContentPane().add(lstSalle);
		
		JLabel lblSalle = new JLabel("Salle :");
		lblSalle.setBounds(38, 157, 46, 25);
		getContentPane().add(lblSalle);
		
		this.lstFilm = new JComboBox<Film>(lesFilms);
		this.lstFilm.setBounds(208, 195, 117, 20);
		getContentPane().add(lstFilm);
		
		JLabel lblFilm = new JLabel("Film :");
		lblFilm.setBounds(38, 193, 58, 22);
		getContentPane().add(lblFilm);
		
		this.lstTarif = new JComboBox<Tarif>(lesTarifs);
		this.lstTarif.setBounds(208, 226, 117, 20);
		getContentPane().add(lstTarif);
		
		JLabel lblTarif = new JLabel("Tarif :");
		lblTarif.setBounds(38, 226, 46, 20);
		getContentPane().add(lblTarif);
		
		this.lstHoraire = new JComboBox<Horaire>(lesHoraires);
		this.lstHoraire.setBounds(208, 257, 117, 20);
		getContentPane().add(lstHoraire);
		
		JLabel lblHoraire = new JLabel("Horaire :");
		lblHoraire.setBounds(38, 257, 58, 20);
		getContentPane().add(lblHoraire);

		this.lesSeances = lesSeances;
		this.lesTarifs = lesTarifs;
		this.lesHoraires = lesHoraires;
		this.lesSalles = lesSalles; 
		this.lesFilms = lesFilms;
	}
	
	public void actionPerformed(ActionEvent evt) 
	{
		Seance laSeance;
		int nb;

		if (evt.getSource() == this.btnAjouter)
		{
			this.uneSalle = (Salle) lstSalle.getSelectedItem();
		
			this.unFilm = (Film) lstFilm.getSelectedItem();
		
			this.unTarif = (Tarif) lstTarif.getSelectedItem();
		
			this.unHoraire = (Horaire) lstHoraire.getSelectedItem();
		
			if (this.txtJour.getText().isEmpty() || this.txtDate.getText().isEmpty() || this.NbrePlacesAchetees.getText().isEmpty())
			{
				JOptionPane.showMessageDialog(null, "Tous les champs sont obligatoires !");
			}
			else
			{
				if(Integer.parseInt(this.NbrePlacesAchetees.getText()) > uneSalle.getNbPlacesTotales())
				{
					JOptionPane.showMessageDialog(null, "Erreur, nombre de places insuffisantes");
				}
				else
				{
					laSeance = new Seance(this.txtJour.getText(), Date.valueOf(txtDate.getText()), Integer.parseInt(this.NbrePlacesAchetees.getText()), uneSalle , unFilm, unHoraire, unTarif);
				
					System.out.println(uneSalle);
												
					nb = DaoProjetCinema.ajouterSeance(laSeance, uneSalle, unTarif, unFilm, unHoraire);
				
					if (nb == 0)
						JOptionPane.showMessageDialog(null, "Cette seance existe déjà !");
					else
					{
						JOptionPane.showMessageDialog(null, "Seance ajoutée.");
						this.lesSeances.add(laSeance);
						this.unFilm.ajouterSeance(laSeance);
					}
				}
			}
		}
	}
}
